package com.heima.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.PartnerMapper;
import com.heima.pojo.dto.PartnerDto;
import com.heima.pojo.dto.PartnerQueryParam;
import com.heima.pojo.domain.PageResult;
import com.heima.pojo.domain.Partner;
import com.heima.pojo.vo.PartnerVo;
import com.heima.service.PartnerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class PartnerServiceImpl extends ServiceImpl<PartnerMapper,Partner> implements PartnerService {

    @Autowired
    private PartnerMapper partnerMapper;

    @Override
    public PageResult select(PartnerQueryParam pqp) {
        PageHelper.startPage(pqp.getPageIndex(),pqp.getPageSize());
        Page<Partner> list = partnerMapper.selectList(pqp.getName());
        return new PageResult<>(pqp.getPageIndex(),pqp.getPageSize(),(long)list.getPages(),list.getTotal(),list.getResult());
    }

    @Override
    public PartnerVo look(Integer id) {
        PartnerVo partnerVo = partnerMapper.look(id);
        return partnerVo;
    }

    @Override
    public Boolean insert(PartnerDto partnerDto) {
        Partner partner = new Partner();
        BeanUtils.copyProperties(partnerDto,partner);

        partner.setStatus(true);
        partner.setCreateTime(LocalDateTime.now());
        partner.setUpdateTime(LocalDateTime.now());
        int x = partnerMapper.insertPartner(partner);
        return x>0;
    }

    @Override
    public Boolean delPartner(Integer id) {
        int res = partnerMapper.delPartner(id);
        return res>0;
    }

    @Override
    public Boolean update(Integer id, PartnerDto partnerDto) {
        int res = partnerMapper.updatePartner(id,partnerDto.getName(),partnerDto.getContact(),partnerDto.getMobile(),partnerDto.getRatio(),partnerDto.getAccount());
        return res>0;
    }
}
